package com.maidou.qiantai.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.maidou.qiantai.common.utils.util.StringUtil;
import com.maidou.qiantai.common.utils.util.WebUtils;
import com.maidou.qiantai.domain.Member;
import com.maidou.qiantai.domain.SpuCommentImage;
import com.maidou.qiantai.dto.spu.SpuCommentDto;
import com.maidou.qiantai.vo.member.MemberVo;
import com.maidou.qiantai.vo.spu.SpuCommentVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maidou.qiantai.mapper.SpuCommentMapper;
import com.maidou.qiantai.domain.SpuComment;
import com.maidou.qiantai.service.ISpuCommentService;

import javax.annotation.Resource;

/**
 * 商品评论Service业务层处理
 * 
 * @author maidou
 * @date 2024-03-13
 */
@Service
public class SpuCommentServiceImpl implements ISpuCommentService 
{
    @Resource
    private SpuCommentMapper spuCommentMapper;
    @Resource
    private SpuCommentImageServiceImpl spuCommentImageService;
    @Resource
    private MemberServiceImpl memberService;


    /**
     * 查询商品评论
     * 
     * @param id 商品评论主键
     * @return 商品评论
     */
    @Override
    public SpuComment selectSpuCommentById(Long id)
    {
        return spuCommentMapper.selectSpuCommentById(id);
    }

    /**
     * 查询商品评论列表
     * 
     * @param spuComment 商品评论
     * @return 商品评论
     */
    @Override
    public List<SpuComment> selectSpuCommentList(SpuComment spuComment)
    {
        return spuCommentMapper.selectSpuCommentList(spuComment);
    }

    /**
     * 新增商品评论
     * 
     * @param spuComment 商品评论
     * @return 结果
     */
    @Override
    public int insertSpuComment(SpuComment spuComment)
    {
        spuComment.setCreateTime(DateUtils.getNowDate());
        return spuCommentMapper.insertSpuComment(spuComment);
    }

    /**
     * 修改商品评论
     * 
     * @param spuComment 商品评论
     * @return 结果
     */
    @Override
    public int updateSpuComment(SpuComment spuComment)
    {
        spuComment.setUpdateTime(DateUtils.getNowDate());
        return spuCommentMapper.updateSpuComment(spuComment);
    }

    /**
     * 批量删除商品评论
     * 
     * @param ids 需要删除的商品评论主键
     * @return 结果
     */
    @Override
    public int deleteSpuCommentByIds(Long[] ids)
    {
        return spuCommentMapper.deleteSpuCommentByIds(ids);
    }

    /**
     * 删除商品评论信息
     * 
     * @param id 商品评论主键
     * @return 结果
     */
    @Override
    public int deleteSpuCommentById(Long id)
    {
        return spuCommentMapper.deleteSpuCommentById(id);
    }

    @Override
    public List<SpuCommentVo> selectSpuCommentListBySpuId(Long spuId) {
        List<SpuComment> spuComments = spuCommentMapper.selectSpuCommentListBySpuId(spuId);
        //一级评论
        List<SpuComment> collect = spuComments.stream().filter((item) -> {
            return item.getCommentId() == 0;
        }).collect(Collectors.toList());
        //二级评论
        List<SpuComment> collectTwo = spuComments.stream().filter((item) -> {
            return item.getCommentId() == 1;
        }).collect(Collectors.toList());
        List<SpuCommentVo> collect1 = collect.stream().map(item -> {
            SpuCommentVo spuCommentVo = new SpuCommentVo();
            ArrayList<SpuCommentVo> spuComments1 = new ArrayList<>();
            for (SpuComment spu : collectTwo
            ) {
                if (item.getId() == spu.getpId()) {
                    SpuCommentVo spuCommentVoItem = new SpuCommentVo();
                    BeanUtils.copyProperties(spu,spuCommentVoItem);
                    //获取用户信息
                    if (spu.getType()==1){
                        Long memberId = spu.getMemberId();
                        Member member = memberService.selectMemberById(memberId);
                        MemberVo memberVo = new MemberVo();
                        BeanUtils.copyProperties(member,memberVo);
                        spuCommentVoItem.setMemberVo(memberVo);

                    }
                    spuComments1.add(spuCommentVoItem);
                }
            }
            BeanUtils.copyProperties(item, spuCommentVo);
            spuCommentVo.setCommentVos(spuComments1);
            //获取图片信息
            String imagesId = item.getImagesId();
            List<Long> longs = StringUtil.tagSplit(imagesId);
            List<SpuCommentImage> images = spuCommentImageService.selectSpuCommentImageByIdBatch(longs);
            spuCommentVo.setSpuCommentImages(images);
            //获取一级用户信息
            if (item.getType()==1){
                Long memberId = item.getMemberId();
                Member member = memberService.selectMemberById(memberId);
                MemberVo memberVo = new MemberVo();
                BeanUtils.copyProperties(member,memberVo);
                spuCommentVo.setMemberVo(memberVo);
            }
            return spuCommentVo;
        }).collect(Collectors.toList());
        return collect1;
    }

    @Override
    public Integer selectSpuCommentNum(Long spuId) {
        return spuCommentMapper.selectSpuCommentNum(spuId);
    }

    @Override
    public Integer selectSpuCommentGoodComment(Long spuId) {
        return spuCommentMapper.selectSpuCommentGoodComment(spuId);
    }



    @Override
    public Integer insertSpuCommentDto(SpuCommentDto spuCommentDto) {
        Long memberId = WebUtils.getMemberIdByAccessToken();
        ArrayList<Long> longs = new ArrayList<>();
        //添加图集数据
        List<String> commentImages = spuCommentDto.getCommentImages();
        for (String url:commentImages
             ) {
            SpuCommentImage spuCommentImage = new SpuCommentImage();
            spuCommentImage.setCommentId(0l);
            spuCommentImage.setDefaultImg(0);
            spuCommentImage.setIsDelete(0);
            spuCommentImage.setCreateTime(new Date());
            spuCommentImage.setImgName(spuCommentDto.getSkuName());
            spuCommentImage.setImgSort("1");
            spuCommentImage.setImgUrl(url);
            spuCommentImageService.insertSpuCommentImage(spuCommentImage);
            longs.add(spuCommentImage.getId());
        }

        String result = String.join(", ", longs.stream()
                .map(Object::toString)
                .toArray(String[]::new));

        //添加评论数据
        SpuComment spuComment = new SpuComment();
        spuComment.setCommentId(0l);
        spuComment.setpId(0l);
        spuComment.setUpvote(spuCommentDto.getSelectedRating().longValue());
        spuComment.setType(1);
        spuComment.setComent(spuCommentDto.getCommentContent());
        spuComment.setRemark(spuCommentDto.getSkuName());
        spuComment.setCreateTime(new Date());
        spuComment.setIsDelete(0);
        spuComment.setSpuId(spuCommentDto.getSpuId());
        spuComment.setMemberId(memberId);
        spuComment.setImagesId(result);

        //添加用户积分
        return spuCommentMapper.insertSpuComment(spuComment);
    }


}
